package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public List<ProductModel> findAll() {
        String sql = "select p.*,c.name cuisineName from product p,cuisine c where p.cuisine_id=c.id order by p.id desc";
        ResultSet resultSet = JDBC.query(sql);
        return setData(resultSet);
    }

    @Override
    public int add(ProductModel productModel) {
        String sql = "insert into product (" +
                "name,price,normal_price,content," +
                "is_show,img_href,cuisine_id," +
                "create_time)values('" + productModel.getName() + "'," +
                "" + productModel.getPrice() + "," +
                "" + productModel.getNormalPrice() + "," +
                "'" + productModel.getContent() + "'," +
                "" + productModel.getIsShow() + "," +
                "'" + productModel.getImgHref() + "'," +
                "" + productModel.getCuisineId() + "," +
                "now())";
        return JDBC.update(sql);
    }

    @Override
    public int del(Integer id) {
        String sql = "delete from product where id=" + id;

        return JDBC.update(sql);
    }

    @Override
    public ProductModel findById(Integer id) {
        String sql = "select p.*,c.name cuisineName from product p,cuisine c " +
                "where p.cuisine_id=c.id and  p.id=" + id;
        ResultSet resultSet = JDBC.query(sql);

        return setData(resultSet).get(0);
    }

    @Override
    public int update(ProductModel productModel) {
        String sql = "update product set name='" + productModel.getName() + "'," +
                "price=" + productModel.getPrice() + "," +
                "normal_price=" + productModel.getNormalPrice() + ",";
        if (!productModel.getContent().equals("")) {
            sql += "content='" + productModel.getContent() + "',";
        }
        if (!productModel.getImgHref().equals("undefined")) {
            sql += "img_href='" + productModel.getImgHref() + "',";
        }
        sql += " remark='' where id=" + productModel.getId();
        return JDBC.update(sql);
    }

    private List<ProductModel> setData(ResultSet resultSet) {
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {

                ProductModel productModel = new ProductModel();
                productModel.setId(resultSet.getInt("id"));
                productModel.setName(resultSet.getString("name"));
                productModel.setNormalPrice(resultSet.getDouble("normal_price"));
                productModel.setPrice(resultSet.getDouble("price"));
                productModel.setContent(resultSet.getString("content"));
                productModel.setIsShow(resultSet.getInt("is_show"));
                productModel.setImgHref(resultSet.getString("img_href"));
                productModel.setCuisineId(resultSet.getInt("cuisine_id"));
                productModel.setCuisineName(resultSet.getString("cuisineName"));
                list.add(productModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
